package cn.itrip.auth.service;

import cn.itrip.auth.config.SDKTestSendTemplateSMS;
import cn.itrip.auth.exception.SMSException;
import cn.itrip.beans.pojo.ItripUser;
import cn.itrip.beans.vo.userinfo.ItripUserVO;
import cn.itrip.common.MD5;
import cn.itrip.common.RedisUtils;
import cn.itrip.dao.mapper.ItripUserMapper;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author Tequila
 **/
@Service
public class ItripUserServiceImpl implements ItripUserService {
    @Autowired
    private ItripUserMapper itripUserMapper;
    @Autowired
    private MailService mailService;
    @Autowired
    private RedisUtils redisUtils;
    @Override
    public boolean ckUsr(String name) {
        QueryWrapper<ItripUser> q = new QueryWrapper<ItripUser>();
        q.eq("userCode",name);
        return itripUserMapper.exists(q);
    }

    private ItripUser viewUserInfo(ItripUserVO itripUserVO){
        ItripUser itripUser = new ItripUser();
        String md5 = MD5.getMd5(itripUserVO.getUserPassword(), 32);// 加密密码
        itripUser.setUserpassword(md5);
        itripUser.setActivated(0);
        itripUser.setUsercode(itripUserVO.getUserCode());
        itripUser.setUsername(itripUserVO.getUserName());
        itripUser.setCreationdate(new Date());
        itripUser.setUsertype(0);
        return itripUser;
    }
    @Override
    public boolean doRegister(ItripUserVO itripUserVO) {

        // 生成一个注册码 发送到你的邮箱
        ItripUser itripUser = viewUserInfo(itripUserVO);
        Integer code = MD5.getRandomCode();

        mailService.sendMail(itripUserVO.getUserCode(),"爱旅行邮箱注册:","请将验证码填写到注册页面,你的爱旅行账号验证码是:"+code);// 往你的邮箱发送右键

        // 将验证码存入到redis中 5分组
        redisUtils.putValue(itripUserVO.getUserCode()+"_mailCode",code.toString(),60*5);

        // 将数据存入msql 中
        return itripUserMapper.insert(itripUser) > 0;
    }

    @Override
    public boolean activate(String user) {

        return itripUserMapper.activate(user) > 0;
    }

    @Autowired
    private SDKTestSendTemplateSMS sdkTestSendTemplateSMS;

    @Override
    public boolean registerByPhone(ItripUserVO itripUserVO) throws SMSException, JsonProcessingException {

        // 1.发短信
        String code = sdkTestSendTemplateSMS.sms(itripUserVO.getUserCode());

        // 2. 将code存入redis中
        redisUtils.putValue(itripUserVO.getUserCode()+"_phone",code,3*60);  // 连接超时
        ItripUser itripUser = viewUserInfo(itripUserVO);
        ObjectMapper json = new ObjectMapper();
        // 3.将用户信息存储到数据库  先将用户信息存入到redis中 如果激活 那就存入到数据库中如果没激活就会自动消失
        redisUtils.putValue(itripUserVO.getUserCode()+"_itripUser", json.writeValueAsString(itripUser),3*60);

        return true;
    }

    @Override
    public boolean validatePhone(String code, String user) throws JsonProcessingException {
        // 1.去redis中查询验证码
        String redisCode = (String) redisUtils.get(user + "_phone");
        // 2.比较
        if (code.equals(redisCode)){

            // 3.去redis中查询对象信息  修改active 1
            String itripUserJson = (String) redisUtils.get(user + "_itripUser");
            // 转换为对象
            ObjectMapper json = new ObjectMapper();
            ItripUser itripUser = json.readValue(itripUserJson, ItripUser.class);
            itripUser.setActivated(1); // 激活用户
            // 4.保存到数据库中
            return  itripUserMapper.insert(itripUser) > 0;
        }

        return false;
    }

    @Override
    public ItripUser doLogin(String name, String password) {
        QueryWrapper<ItripUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userCode",name);
        queryWrapper.eq("userPassword",password);

        return itripUserMapper.selectOne(queryWrapper);
    }


}
